package 深市_沪市;

import Moudle.Item;
import Moudle.ItemSpecial;
import Utils.HttpConnect;
import Utils.Tools;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.mongodb.BasicDBObject;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.UpdateOptions;
import org.apache.http.ProtocolException;
import org.bson.Document;

import java.text.SimpleDateFormat;
import java.util.*;

public class Main2 {
    /**
     * 合并所有数据
     */
    static Map<String, String> header_map = new HashMap<>();
    static Map<String, String> data_map = new HashMap<>();
    static String url = "http://www.cninfo.com.cn/new/hisAnnouncement/query";
    static MongoClient mongoClient = new MongoClient("192.168.1.173", 27017);
    static MongoDatabase mDatabase = mongoClient.getDatabase("JuChao");
    static MongoCollection mCollection = mDatabase.getCollection("沪市_年报_2006-06-28");
    static MongoCollection dateCollection = mDatabase.getCollection("沪市_年报_2006-06-28(日期)");
    static String searchkey;

    static {
        data_map.put("pageNum", "1");
        data_map.put("pageSize", "30");
        data_map.put("column", "szse");
        data_map.put("tabName", "fulltext");
        data_map.put("plate", "sh"); //深市——沪市
        data_map.put("stock", "");
        data_map.put("searchkey", "");
        data_map.put("secid", "");
        data_map.put("category", "");
        data_map.put("trade", "");
        data_map.put("seDate", "2021-01-01~2021-05-20");
        data_map.put("sortName", "");
        data_map.put("sortType", "");
        data_map.put("isHLtitle", "true");

        header_map.put("Origin", "http://www.cninfo.com.cn");
        header_map.put("Referer", "http://www.cninfo.com.cn/new/fulltextSearch?notautosubmit=&keyWord=%E7%8B%AC%E8%91%A3%E6%84%8F%E8%A7%81");
    }

    public static void main(String[] args) throws ProtocolException {
        List<String> dateList = Tools.getDateList("2000-06-28", "2021-06-23");
        dateList.forEach(date -> {
            try {
                GetJsonStr(date, 1);
            } catch (ProtocolException e) {
                e.printStackTrace();
            }
        });
    }

    static void GetJsonStr(String time, int pageNum) throws ProtocolException {
        data_map.put("seDate", time + "~" + time );
        data_map.put("pageNum", String.valueOf(pageNum));

        String jsonStr = HttpConnect.HttpPost_OkHttp(url, header_map, data_map);
        JSONObject jsonObject = JSON.parseObject(jsonStr);

        try {
            if (jsonObject.getJSONObject("announcements") == null) {
                return;
            }
        } catch (ClassCastException e) {
            System.out.println(time + "  有数据");
            Document document = new Document("_id", time);
            BasicDBObject query = new BasicDBObject().append("_id", time);
            BasicDBObject obj = new BasicDBObject("$set", document);
            dateCollection.updateOne(query, obj, (new UpdateOptions()).upsert(true));
        }


        JsonToDb(jsonObject, time, String.valueOf(pageNum));
        if (jsonObject.getString("hasMore").equals("true"))
            if (pageNum == 100){
                MongoCollection errorColl = mDatabase.getCollection("沪市超100页");
                Document document = new Document("_id", time)
                        .append("totalAnnouncement", jsonObject.getString("totalAnnouncement"));
                BasicDBObject query = new BasicDBObject().append("_id", time);
                BasicDBObject obj = new BasicDBObject("$set", document);
                errorColl.updateOne(query, obj, (new UpdateOptions()).upsert(true));
                return;
            }

        GetJsonStr(time, pageNum + 1);
    }

    static void JsonToDb(JSONObject JSONOBJECT, String time, String pageNum) throws ProtocolException {

        JSONArray announcements = JSONOBJECT.getJSONArray("announcements");
        List<ItemSpecial> list = new ArrayList<>();
        int n = announcements.size();

        for (int i = 0; i < n; i++) {
            JSONObject jsonObject = announcements.getJSONObject(i);
            ItemSpecial item = new ItemSpecial();
            item.adjunctSize = jsonObject.getString("adjunctSize");
            item.adjunctType = jsonObject.getString("adjunctType");
            item.adjunctUrl = jsonObject.getString("adjunctUrl");
            item.announcementContent = jsonObject.getString("announcementContent");
            item.announcementId = jsonObject.getString("announcementId");
            item.announcementTime = jsonObject.getString("announcementTime");
            item.announcementTitle = jsonObject.getString("announcementTitle");
            item.announcementType = jsonObject.getString("announcementType");
            item.announcementTypeName = jsonObject.getString("announcementTypeName");
            item.associateAnnouncement = jsonObject.getString("associateAnnouncement");
            item.batchNum = jsonObject.getString("batchNum");
            item.columnId = jsonObject.getString("columnId");
            item.id = jsonObject.getString("id");
            item.important = jsonObject.getString("important");
            item.orgId = jsonObject.getString("orgId");
            item.orgName = jsonObject.getString("orgName");
            item.pageColumn = jsonObject.getString("pageColumn");
            item.secCode = jsonObject.getString("secCode");
            item.secName = jsonObject.getString("secName");
            item.storageTime = jsonObject.getString("storageTime");

            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            item.公告时间 = format.format(new Date(new Long(item.announcementTime)));
            item.抓取时间 = format.format(System.currentTimeMillis());

            list.add(item);
        }

        for (ItemSpecial item : list) {
            Document doc = new Document("_id", item.announcementId)
                    .append("adjunctSize", item.adjunctSize)
                    .append("adjunctType", item.adjunctType)
                    .append("adjunctUrl", item.adjunctUrl)
                    .append("announcementContent", item.announcementContent)
                    .append("announcementId", item.announcementId)
                    .append("announcementTime", item.announcementTime)
                    .append("announcementTitle", item.announcementTitle)
                    .append("announcementType", item.announcementType)
                    .append("announcementTypeName", item.announcementTypeName)
                    .append("associateAnnouncement", item.associateAnnouncement)
                    .append("batchNum", item.batchNum)
                    .append("columnId", item.columnId)
                    .append("id", item.id)
                    .append("important", item.important)
                    .append("orgId", item.orgId)
                    .append("orgName", item.orgName)
                    .append("pageColumn", item.pageColumn)
                    .append("secCode", item.secCode)
                    .append("secName", item.secName)
                    .append("storageTime", item.storageTime)
                    .append("公告时间", item.公告时间)
                    .append("抓取时间", item.抓取时间);

            BasicDBObject obj = new BasicDBObject("$set", doc);
            BasicDBObject query = new BasicDBObject().append("_id", item.announcementId);
            mCollection.updateOne(query, obj, (new UpdateOptions()).upsert(true));
            System.out.println(doc.getString("announcementId") + " - " + doc.getString("secName") + " 写入数据库");
        }
    }
}
